/*
 * Zettelkasten - nach Luhmann
 * Copyright (C) 2001-2015 by Daniel Lüdecke (http://www.danielluedecke.de)
 * 
 * Homepage: http://zettelkasten.danielluedecke.de
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU
 * General Public License, wie von der Free Software Foundation veröffentlicht, weitergeben
 * und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder (wenn Sie möchten)
 * jeder späteren Version.
 * 
 * Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein 
 * wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder 
 * der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Details finden Sie in der 
 * GNU General Public License.
 * 
 * Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm 
 * erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>.
 */
package de.danielluedecke.zettelkasten.tasks;

import de.danielluedecke.zettelkasten.EntryID;
import de.danielluedecke.zettelkasten.database.Daten;

/**
 *
 * @author Luedeke
 */
public class EntriesToLuhmannTask extends org.jdesktop.application.Task<Object, Void> {
	/**
	 * Reference to the main data class
	 */
	private final Daten dataObj;
	/**
	 * 
	 */
	int[] entries;
	/**
	 * 
	 */
	int insertPos;
	private final javax.swing.JDialog parentDialog;
	private final javax.swing.JLabel msgLabel;
	/**
	 * get the strings for file descriptions from the resource map
	 */
	private final org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
			.getInstance(de.danielluedecke.zettelkasten.ZettelkastenApp.class).getContext()
			.getResourceMap(EntriesToLuhmannTask.class);

	EntriesToLuhmannTask(org.jdesktop.application.Application app, javax.swing.JDialog parent, javax.swing.JLabel label,
			Daten d, int[] entr, int insertpos) {
		// Runs on the EDT. Copy GUI state that
		// doInBackground() depends on from parameters
		// to createLinksTask fields, here.
		super(app);

		dataObj = d;
		parentDialog = parent;
		msgLabel = label;
		entries = entr;
		insertPos = insertpos;
		// init status text
		msgLabel.setText(resourceMap.getString("msg1"));
	}

	@Override
	protected Object doInBackground() {
		// Your Task's code here. This method runs
		// on a background thread, so don't reference
		// the Swing GUI from here.

		// get amount of entries
		int len = entries.length;

		// go through complete data set
		for (int cnt = 0; cnt < len; cnt++) {
			// leave out current entry
			if (insertPos == entries[cnt]) {
				continue;
			}
			// add it to the current entry's luhmann-numbers
			dataObj.appendSubEntryToEntry(new EntryID(insertPos), new EntryID(entries[cnt]));
			// update progressbar
			setProgress(cnt, 0, len);
		}

		return null;
	}

	@Override
	protected void succeeded(Object result) {
		// Runs on the EDT. Update the GUI based on
		// the result computed by doInBackground().
	}

	@Override
	protected void finished() {
		super.finished();
		// and close window
		parentDialog.dispose();
		parentDialog.setVisible(false);
	}
}
